package com.qingcheng.controller.file;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

@RestController
@RequestMapping("/upload")
public class UploadController {

    //D:\qingcheng\qingcheng_parent\qingcheng_web_manager\src\main\java\com\qingcheng\controller\file\UploadController.java
    //@Autowired
    //private HttpServletRequest request;

    @PostMapping("/native")
    public String nativeUpload(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        String path=request.getSession().getServletContext().getRealPath("img");
        String filePath = path +"/"+ file.getOriginalFilename();
        File desFile = new File(filePath);
        if(!desFile.getParentFile().exists()){
            desFile.mkdirs();
        }
        try {
            file.transferTo(desFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("path:---"+filePath);
        return "http://localhost:9101/img/"+file.getOriginalFilename();
    }



}
